package com.feicheng.controller.wx;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.common.sms.MSGContant;
import com.feicheng.common.util.StringGenerator;
import com.feicheng.persistence.entity.Amsg;
import com.feicheng.persistence.mapper.AmsgMapper;
import com.feicheng.persistence.service.IAmsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController("/wx/msg")
@RequestMapping("/wx/msg")
public class MsgController {

    @Autowired
    IAmsgService amsgService;

    @Autowired
    MSGContant contant;

    @PostMapping("/chat/clear")
    public ResultInfo clear(@RequestBody JSONObject param) {
        String fromid = param.getString("fromid");
        String toid = param.getString("toid");
        contant.clearMsg(toid, fromid);
        return ResultInfo.success();
    }

    @PostMapping("/chat/read")
    public ResultInfo read(@RequestBody JSONObject param) {
        String fromid = param.getString("fromid");
        String toid = param.getString("toid");
        param.put("msg", contant.getMsg(toid, fromid));
        return ResultInfo.success(param);
    }

    @PostMapping("/chat/write")
    public ResultInfo write(@RequestBody JSONObject param) {
        Amsg amsg = param.getObject("msg", Amsg.class);
        amsg = contant.pushMsg(amsg);
        param.put("msg", amsg);
        return ResultInfo.success(param);
    }

    @PostMapping("/chat/list")
    public ResultInfo chatlist(@RequestBody JSONObject param) {
        QueryWrapper<Amsg> qw = new QueryWrapper<>();
        String fromid = param.getString("fromid");
        String toid = param.getString("toid");

        qw.or(q -> {
            q.eq("fromid", fromid);
            q.eq("toid", toid);
        });

        qw.or(q -> {
            q.eq("toid", fromid);
            q.eq("fromid", toid);
        });

        qw.orderByDesc("ct");

        Integer current = param.getInteger("current");
        if (null == current) {
            current = 1;
        }

        Integer size = param.getInteger("size");
        if (null == size) {
            size = 10;
        }
        Page<Amsg> page = new Page<>(current, size);
        page = amsgService.page(page, qw);
        param.put("msgs", page);

        return ResultInfo.success(param);
    }



    @PostMapping("/post")
    public ResultInfo newMsgs(@RequestBody JSONObject param) {
        Amsg msg = param.getObject("msg", Amsg.class);
        msg.setMsgid(StringGenerator.getID("MSG"));
        msg.setCt(LocalDateTime.now());
        msg.setReadflg(false);
        amsgService.save(msg);
        param.put("msg", msg);
        return ResultInfo.success(param);
    }

    @PostMapping("/put")
    public ResultInfo updateMsgs(@RequestBody JSONObject param) {
        Amsg msg = param.getObject("msg", Amsg.class);
        amsgService.updateById(msg);
        param.put("msg", msg);
        return ResultInfo.success(param);
    }

    @PostMapping("/delete")
    public ResultInfo rmMsgs(@RequestBody JSONObject param) {
        String msgid = param.getString("msgid");
        Amsg msg = amsgService.getById(msgid);
        amsgService.removeById(msgid);
        param.put("msg", msg);
        return ResultInfo.success(param);
    }

    @PostMapping("/list")
    public ResultInfo getMsgs(@RequestBody JSONObject param) {

        QueryWrapper<Amsg> qw = new QueryWrapper<>();

        String fromid = param.getString("fromid");
        if (StringUtils.hasLength(fromid)) {
            qw.eq("fromid", fromid);
        }

        String toid = param.getString("toid");
        if (StringUtils.hasLength(toid)) {
            qw.eq("toid", toid);
        }

        Boolean readflg = param.getBoolean("readflg");
        if (null != readflg) {
            qw.eq("readflg", readflg);
        }

        String sort = param.getString("sort");
        if ("a".equals(sort)) {
            qw.orderByAsc("ct");
        } else if ("d".equals(sort)) {
            qw.orderByDesc("ct");
        }

        String groupBy = param.getString("groupby");
        if (StringUtils.hasLength(groupBy)) {
            qw.last(groupBy);
        }


        List<Amsg> msgs = amsgService.list(qw);
        param.put("msgs", msgs);

        return ResultInfo.success(param);
    }

    @PostMapping("/page")
    public ResultInfo getMsgsByPage(@RequestBody JSONObject param) {

        Integer current = param.getInteger("current");
        if (null == current) {
            current = 1;
        }

        Integer size = param.getInteger("size");
        if (null == size) {
            size = 10;
        }

        String toid = param.getString("toid");
        if (!StringUtils.hasLength(toid)) {
            throw new SystemException("必须指定消息来源");
        }

        Page<JSONObject> page = new Page<>(current, size);

        AmsgMapper amsgMapper = (AmsgMapper) amsgService.getBaseMapper();

        page = amsgMapper.getMsgClient(page, toid);
        param.put("msgs", page);

        return ResultInfo.success(param);
    }


}
